* Bernd Beber, Michael J. Gilligan, Jenny Guardado, and Sabrina Karim
* Stata/MP 15 code to prepare data for "The Promise and Peril of Peacekeeping Economies"

* input datasets (e.g. World Development Indicators) are NOT included with the replication materials
* this code does not run without them, but shows how the data was harmonized and merged, variables were constructed, etc.

* cross-country data

* prepare World Development Indicators (WDI) data, version September 9, 2015
* in raw Excel data, we prefixed "data" to any column names that denote years, then saved as WDI_prefix.dta in folder WDI
* load data
    use "WDI/WDI_prefix"

* reshape to move years into rows
    gen id = _n
    reshape long data, i(id) j(year)

* reshape to move indicators into columns
    replace indicatorcode = subinstr(indicatorcode, ".", "_", .)
    drop id indicatorname
    reshape wide data, i(countryname countrycode year) j(indicatorcode) string
    rename data* *
    save WDI/WDI_wide.dta, replace

* second, prepare UCDP/PRIO conflict data
    * include Battle Deaths Dataset 1946–2008, version 3.0, September 2009
    use "Battle deaths/PRIO_bd3.0.dta"
    rename id conflictid
    tostring conflictid, replace
    replace conflictid = "1-" + conflictid
    rename location locationbd
    tempfile bd
    save `bd'
    * merge into UCDP/PRIO Armed Conflict Dataset, version 4-2015
    use "UCDP/124920_1ucdp-prio-_2015.dta"
    merge 1:1 conflictid year using `bd'
    * include battle deaths for UCDP/PRIO-listed cases only
    gen bdeadnoentry=1 if _merge==1
    drop if _merge==2
    drop _merge locationbd
    order bdeadlow bdeadhig bdeadbes bdeadnoentry annualdata source bdversion, after(cumulativeintensity)

* create one observation for each country for conflicts that are located in multiple countries
    * correct instance in which comma was not used to separate distinct conflict locations
    replace location="Macedonia (FYR)" if location=="Macedonia, FYR"
    replace location="India" if location=="Hyderabadh, India"
    replace location="India" if location=="Hyderabadh"
    * group conflict locations that are single country in current WDI database
    replace location="Vietnam" if location=="South Vietnam, Vietnam (North Vietnam)"
    replace location="Vietnam" if location=="South Vietnam"
    replace location="Vietnam" if location=="Vietnam (North Vietnam)"
    replace location="Cambodia (Kampuchea), Vietnam" if location=="Cambodia (Kampuchea), Vietnam (North Vietnam)"
    replace location="China, Vietnam" if location=="China, Vietnam (North Vietnam)"
    replace location="Yemen" if location=="South Yemen, Yemen (North Yemen)"
    replace location="Yemen" if location=="South Yemen"
    replace location="Yemen" if location=="Yemen (North Yemen)"
    * use function -noccur- from -egenmore- to separate conflict locations
    * ssc inst egenmore
    egen lcount = noccur(location), string(",")
    replace lcount = lcount + 1
    expand lcount
    sort conflictid year
    by conflictid year: gen counter = _n
    split location, p(", ")
    gen countryname = ""
    forvalues i = 1/6 {
        replace countryname = location`i' if counter==`i'
    }
    label var countryname "Country name"
    drop lcount counter location1-location6

* reshape multiple conflicts per country-year to wide format
    bysort countryname year: gen counter = _n
    order version countryname year counter
    for varlist conflictid-region: rename X X_c
    reshape wide conflictid-region, i(countryname year) j(counter)
    order countryname year version
    forvalues i = 1/8 {
        label var conflictid_c`i' "ConflictId, conflict `i'"
        label var location_c`i' "Location, conflict `i'"
        label var sidea_c`i' "SideA, conflict `i'"
        label var sidea2nd_c`i' "SideA2nd, conflict `i'"
        label var sideb_c`i' "SideB, conflict `i'"
        label var sidebid_c`i' "SideBID, conflict `i'"
        label var sideb2nd_c`i' "SideB2nd, conflict `i'"
        label var incompatibility_c`i' "Incompatibility, conflict `i'"
        label var territoryname_c`i' "TerritoryName, conflict `i'"
        label var intensitylevel_c`i' "IntensityLevel, conflict `i'"
        label var cumulativeintensity_c`i' "CumulativeIntensity, conflict `i'"
        label var bdeadlow_c`i' "bdeadlow, conflict `i'"
        label var bdeadhig_c`i' "bdeadhig, conflict `i'"
        label var bdeadbes_c`i' "bdeadbes, conflict `i'"
        label var bdeadnoentry_c`i' "bdeadnoentry, conflict `i'"
        label var annualdata_c`i' "annualdata, conflict `i'"
        label var source_c`i' "source, conflict `i'"
        label var bdversion_c`i' "bdversion, conflict `i'"
        label var typeofconflict_c`i' "TypeOfConflict, conflict `i'"
        label var startdate_c`i' "StartDate, conflict `i'"
        label var startprec_c`i' "StartPrec, conflict `i'"
        label var startdate2_c`i' "StartDate2, conflict `i'"
        label var startprec2_c`i' "StartPrec2, conflict `i'"
        label var epend_c`i' "EpEnd, conflict `i'"
        label var ependdate_c`i' "EpEndDate, conflict `i'"
        label var ependprec_c`i' "EpEndPrec, conflict `i'"
        label var gwnoa_c`i' "GWNoA, conflict `i'"
        label var gwnoa2nd_c`i' "GWNoA2nd, conflict `i'"
        label var gwnob_c`i' "GWNoB, conflict `i'"
        label var gwnob2nd_c`i' "GWNoB2nd, conflict `i'"
        label var gwnoloc_c`i' "GWNoLoc, conflict `i'"
        label var region_c`i' "Region, conflict `i'"
    }

* bring state identifier in line with WDI data
    replace countryname="Bosnia and Herzegovina" if countryname=="Bosnia-Herzegovina"
    replace countryname="Brunei Darussalam" if countryname=="Brunei"
    replace countryname="Cambodia" if countryname=="Cambodia (Kampuchea)"
    replace countryname="Congo, Dem. Rep." if countryname=="DR Congo (Zaire)"
    replace countryname="Congo, Rep." if countryname=="Congo"
    replace countryname="Iran, Islamic Rep." if countryname=="Iran"
    replace countryname="Egypt, Arab Rep." if countryname=="Egypt"
    replace countryname="Macedonia, FYR" if countryname=="Macedonia (FYR)"
    replace countryname="Gambia, The" if countryname=="Gambia"
    replace countryname="Cote d'Ivoire" if countryname=="Ivory Coast"
    replace countryname="Lao PDR" if countryname=="Laos"
    replace countryname="Madagascar" if countryname=="Madagascar (Malagasy)"
    replace countryname="Myanmar" if countryname=="Myanmar (Burma)"
    replace countryname="Korea, Dem. Rep." if countryname=="North Korea"
    replace countryname="Romania" if countryname=="Rumania"
    replace countryname="Russian Federation" if countryname=="Russia (Soviet Union)"
    replace countryname="Serbia" if countryname=="Serbia (Yugoslavia)"
    replace countryname="Korea, Rep." if countryname=="South Korea"
    replace countryname="Yemen, Rep." if countryname=="Yemen"
    replace countryname="Syrian Arab Republic" if countryname=="Syria"
    replace countryname="Taiwan, China" if countryname=="Taiwan"
    replace countryname="United States" if countryname=="United States of America"
    replace countryname="Venezuela, RB" if countryname=="Venezuela"
    replace countryname="Zimbabwe" if countryname=="Zimbabwe (Rhodesia)"

* save
    tempfile ucdp
    save `ucdp'

* third, prepare data on Third-Party Peacekeeping Missions, 1946-2014, version 3.1
    use "Third-Party PKMs/Third-Party-PKMs-version-3.1.dta"

* bring state identifier in line with WDI data
    replace state="Bosnia and Herzegovina" if state=="Bosnia-Herzegovina"
    replace state="Comoros" if state=="Comoros/Anjouan & Moheli islands"
    replace state="Congo, Dem. Rep." if state=="Congo-Kinshasa/Zaire/DRC"
    replace state="Croatia" if state=="Croatia/Serbians"
    replace state="Timor-Leste" if state=="East Timor"
    replace state="Cambodia" if state=="France/Indochina/Cambodia"
    replace state="Laos" if state=="France/Indochina/Laos"
    replace state="Vietnam" if state=="France/Indochina/Vietnam"
    replace state="Georgia" if state=="Georgia/Abkhazia"
    replace state="Georgia" if state=="Georgia/South Ossetia"
    replace state="Indonesia" if state=="Indonesia/Aceh"
    replace state="Timor-Leste" if state=="Indonesia/East Timor"
    replace state="Iraq" if state=="Iraq/Kurds"
    replace state="Moldova" if state=="Moldova/Trans-Dnestr"
    replace state="Morocco" if state=="Morocco/Western Saharan"
    replace state="Indonesia" if state=="Netherlands/Dutch East Indies"
    replace state="Yemen, Rep." if state=="North Yemen"
    replace state="Papua New Guinea" if state=="Papua New Guinea/Bougainville"
    replace state="Philippines" if state=="Philippines/Sulu, Palawan, and Mindanao Islands"
    replace state="South Sudan" if state=="Republic of South Sudan"
    replace state="Zimbabwe" if state=="Rhodesia/Zimbabwe"
    replace state="Kosovo" if state=="Serbia/Kosovo"
    replace state="Namibia" if state=="South Africa/Namibia"
    replace state="Vietnam" if state=="South Vietnam"
    replace state="Sri Lanka" if state=="Sri Lanka/Tamils"
    replace state="Sudan" if state=="Sudan/Darfur"
    replace state="South Sudan" if state=="Sudan/SPLM"
    replace state="Croatia" if state=="Yugoslavia/Croatia"
    replace state="Slovenia" if state=="Yugoslavia/Slovenia"
    replace state="Lao PDR" if state=="Laos"
    replace state="Macedonia, FYR" if state=="Macedonia"
    replace state="Korea, Rep." if state=="South Korea"
    replace state="Syrian Arab Republic" if state=="Syria"

* expand dataset to country-year observations
    * how many yearly observations
    gen n = endyr - startyr + 1
    * also for conflicts that are ongoing
    replace n = 2015 - startyr + 1 if endyr==.
    * expand so there are as many rows as years
    expand n
    bysort obsnum: gen counter = _n
    gen year = startyr + counter - 1
    sort obsnum year
    drop counter n

* reshape multiple missions per country-year to wide format
    rename state countryname
    bysort countryname year: gen counter = _n
    order countryname year counter
    for varlist obsnum-mediate2: rename X X_c
    reshape wide obsnum-mediate2, i(countryname year) j(counter)
    forvalues i = 1/6 {
        label var obsnum_c`i' "obsnum, PKM `i'"
        label var ccode1_c`i' "ccode1, PKM `i'"
        label var distype_c`i' "distype, PKM `i'"
        label var regnum_c`i' "regnum, PKM `i'"
        label var disnum_c`i' "disnum, PKM `i'"
        label var tptype_c`i' "tptype, PKM `i'"
        label var unmandate_c`i' "unmandate, PKM `i'"
        label var unresoldate_c`i' "unresoldate, PKM `i'"
        label var pkmnamelocation_c`i' "pkmnamelocation, PKM `i'"
        label var purpose1_c`i' "purpose1, PKM `i'"
        label var purpose2_c`i' "purpose2, PKM `i'"
        label var purpose3_c`i' "purpose3, PKM `i'"
        label var purpose4_c`i' "purpose4, PKM `i'"
        label var leadstate_c`i' "leadstate, PKM `i'"
        label var ccode2_c`i' "ccode2, PKM `i'"
        label var startday_c`i' "startday, PKM `i'"
        label var startmon_c`i' "startmon, PKM `i'"
        label var startyr_c`i' "startyr, PKM `i'"
        label var endday_c`i' "endday, PKM `i'"
        label var endmon_c`i' "endmon, PKM `i'"
        label var endyr_c`i' "endyr, PKM `i'"
        label var cease_c`i' "cease, PKM `i'"
        label var peace_c`i' "peace, PKM `i'"
        label var pkmnum1_c`i' "pkmnum1, PKM `i'"
        label var pkmnum2_c`i' "pkmnum2, PKM `i'"
        label var statenum_c`i' "statenum, PKM `i'"
        label var fatal1_c`i' "fatal1, PKM `i'"
        label var fatal2_c`i' "fatal2, PKM `i'"
        label var duration_c`i' "duration, PKM `i'"
        label var ongoing_c`i' "ongoing, PKM `i'"
        label var milhost1_c`i' "milhost1, PKM `i'"
        label var milhost2_c`i' "milhost2, PKM `i'"
        label var treaty_c`i' "treaty, PKM `i'"
        label var mediate1_c`i' "mediate1, PKM `i'"
        label var mediate2_c`i' "mediate2, PKM `i'"
    }

* save
    compress
    tempfile pkm
    save `pkm'

* fourth, data from James Fearon and David Laitin, 2003, "Ethnicity, Insurgency, and Civil War," American Political Science Review 97(1): 75-90
    use "Fearon and Laitin/repdata"
    keep country year mtnest lmtnest elevdiff ethfrac relfrac colbrit colfra
    * these variables are not time-varying
    collapse (first) mtnest lmtnest elevdiff ethfrac relfrac colbrit colfra, by(country)

* bring country identifier in line with WDI data
    rename country countryname
    replace countryname=proper(countryname)
    replace countryname="Bosnia and Herzegovina" if countryname=="Bosnia"
    replace countryname="Myanmar" if countryname=="Burma"
    replace countryname="Central African Republic" if countryname=="Central African Rep."
    replace countryname="Congo, Rep." if countryname=="Congo"
    replace countryname="Costa Rica" if countryname=="Costarica"
    replace countryname="Czech Republic" if countryname=="Czechrep"
    replace countryname="Congo, Dem. Rep." if countryname=="Dem. Rep. Congo"
    replace countryname="Dominican Republic" if countryname=="Dominican Rep."
    replace countryname="Egypt, Arab Rep." if countryname=="Egypt"
    replace countryname="Gambia, The" if countryname=="Gambia"
    replace countryname="Germany" if countryname=="Germanyfed. Rep."
    replace countryname="Guinea-Bissau" if countryname=="Guinea Bissau"
    replace countryname="Iran, Islamic Rep." if countryname=="Iran"
    replace countryname="Cote d'Ivoire" if countryname=="Ivory Coast"
    replace countryname="Korea, Rep." if countryname=="Korea, S."
    replace countryname="Kyrgyz Republic" if countryname=="Kyrgyzstan"
    replace countryname="Lao PDR" if countryname=="Laos"
    replace countryname="Macedonia, FYR" if countryname=="Macedonia"
    replace countryname="Korea, Dem. Rep." if countryname=="N. Korea"
    replace countryname="Papua New Guinea" if countryname=="Papua N.G."
    replace countryname="Russian Federation" if countryname=="Russia"
    replace countryname="Slovak Republic" if countryname=="Slovakia"
    replace countryname="Syrian Arab Republic" if countryname=="Syria"
    replace countryname="Taiwan, China" if countryname=="Taiwan"
    replace countryname="Trinidad and Tobago" if countryname=="Trinidad & Tobago"
    replace countryname="United Arab Emirates" if countryname=="U. Arab Emirates"
    replace countryname="United Kingdom" if countryname=="Uk"
    replace countryname="United States" if countryname=="Usa"
    replace countryname="Venezuela, RB" if countryname=="Venezuela"
    replace countryname="Yemen, Rep." if countryname=="Yemen"

* save
    compress
    tempfile fl
    save `fl'

* merge
    use WDI/WDI_wide
    merge 1:1 countryname year using `ucdp'
    drop _merge
    merge 1:1 countryname year using `pkm'
    drop _merge
    merge m:1 countryname using `fl'
    drop if _merge==2
    drop _merge

* drop regional aggregate measures
    drop if countryname=="Arab World"
    drop if countryname=="Caribbean small states"
    drop if countryname=="Central Europe and the Baltics"
    drop if countryname=="East Asia & Pacific (all income levels)"
    drop if countryname=="East Asia & Pacific (developing only)"
    drop if countryname=="Euro area"
    drop if countryname=="Europe & Central Asia (all income levels)"
    drop if countryname=="Europe & Central Asia (developing only)"
    drop if countryname=="European Union"
    drop if countryname=="Fragile and conflict affected situations"
    drop if countryname=="Heavily indebted poor countries (HIPC)"
    drop if countryname=="High income"
    drop if countryname=="High income: OECD"
    drop if countryname=="High income: nonOECD"
    drop if countryname=="Latin America & Caribbean (all income levels)"
    drop if countryname=="Latin America & Caribbean (developing only)"
    drop if countryname=="Least developed countries: UN classification"
    drop if countryname=="Low & middle income"
    drop if countryname=="Low income"
    drop if countryname=="Lower middle income"
    drop if countryname=="Middle East & North Africa (all income levels)"
    drop if countryname=="Middle East & North Africa (developing only)"
    drop if countryname=="Middle income"
    drop if countryname=="North America"
    drop if countryname=="Not classified"
    drop if countryname=="OECD members"
    drop if countryname=="Other small states"
    drop if countryname=="Pacific island small states"
    drop if countryname=="Small states"
    drop if countryname=="South Asia"
    drop if countryname=="Sub-Saharan Africa (all income levels)"
    drop if countryname=="Sub-Saharan Africa (developing only)"
    drop if countryname=="Upper middle income"
    drop if countryname=="World"

* create variables for analysis
    * conflict indicators
    * UCDP coverage extends from 1946 to 2014

    * any conflict
    gen conflict=1 if conflictid_c1!=""
    replace conflict=0 if conflictid_c1=="" & year<2015

    * ever major conflict, i.e. more than 1000 fatalities in a year
    gen majorconflict=0 if year<2015
    forvalues i = 1/8 {
        replace majorconflict=1 if conflictid_c`i'!="" & intensitylevel_c`i'==2
    }
    tempfile evermajor
    preserve
        collapse (sum) majorconflict, by(countryname)
        rename majorconflict evermajorconflict
        replace evermajorconflict=1 if evermajorconflict>1 & evermajorconflict!=.
        save `evermajor'
    restore
    merge m:1 countryname using `evermajor'
    drop _merge

    * number of last ten years in conflict
    tempfile lastten
    forvalues u = 1955/2014 {
        local d = `u' - 9
        preserve
            collapse (sum) conflict if year>=`d' & year<=`u', by(countryname)
            gen year=`u'
            rename conflict past10conflict
            cap append using `lastten'
            save `lastten', replace
        restore
    }
    merge 1:1 countryname year using `lastten'
    drop if _merge==2
    drop _merge

    * number of last five years in conflict
    tempfile lastfive
    forvalues u = 1950/2014 {
        local d = `u' - 4
        preserve
            collapse (sum) conflict if year>=`d' & year<=`u', by(countryname)
            gen year=`u'
            rename conflict past5conflict
            cap append using `lastfive'
            save `lastfive', replace
        restore
    }
    merge 1:1 countryname year using `lastfive'
    drop if _merge==2
    drop _merge

    * battle deaths indicators
    * coverage extends to 2008

    * total battle deaths current year, logged
    for varlist bdeadbes_c*: recode X (-999 = .), gen(Xtemp)
    egen deaths=rowtotal(bdeadbes_c1temp-bdeadbes_c8temp)
    egen temp=rownonmiss(bdeadbes_c1temp-bdeadbes_c8temp)
    replace deaths=. if (temp==0 & conflict==1) | year>2008
    gen lndeaths=deaths+1
    replace lndeaths=log(lndeaths)
    drop bdeadbes_c1temp-bdeadbes_c8temp temp

    * total battle deaths last five years, logged
    tempfile lastfive
    forvalues u = 1950/2008 {
        local d = `u' - 4
        preserve
            collapse (sum) past5deaths=deaths (count) temp=deaths if year>=`d' & year<=`u', by(countryname)
            replace past5deaths=. if temp!=5
            drop temp
            gen year=`u'
            cap append using `lastfive'
            save `lastfive', replace
        restore
    }
    merge 1:1 countryname year using `lastfive'
    drop if _merge==2
    drop _merge
    gen lnpast5deaths=past5deaths+1
    replace lnpast5deaths=log(lnpast5deaths)

    * peacekeeping missions (PKM) indicators
    * coverage extends to 2015

    * any UN PKM
    gen unpkm=0
    forvalues i = 1/6 {
        replace unpkm=1 if obsnum_c`i'!=. & tptype_c`i'==1
    }

    * total UN personnel deployed, logged
    forvalues i = 1/6 {
        egen temp`i'=rowtotal(pkmnum*_c`i') if obsnum_c`i'!=. & tptype_c`i'==1
    }
    egen depunpkm=rowtotal(temp1-temp6)
    gen lndepunpkm=depunpkm + 1
    replace lndepunpkm=log(lndepunpkm)
    drop temp1-temp6

    * economic indicators
    * GDP per capita growth (annual %)
    gen growthpc=NY_GDP_PCAP_KD_ZG
    * infant mortality
    gen infantmort=SP_DYN_IMRT_IN
    * GDP (constant 2005 US$)
    gen gdp=NY_GDP_MKTP_KD

    * other variables
    * log population
    gen lnpop=log(SP_POP_TOTL)

    * fearon and laitin coverage does not extend to all small countries and countries formed after 1999
    * ethnic fractionalization
    tab ethfrac
    * log mountainous terrain
    tab lmtnest
    * former british colony
    tab colbrit
    * former french colony
    tab colfra

    * numeric country identifier
    encode countryname, gen(countrynum)
    label val countrynum
    label drop countrynum

* remove note from merged dataset
    note drop _dta

* retain, label, and order variables for analysis
    label var countryname "Country name"
    label var countrynum "Numeric country identifier"
    label var year "Year"
    label var growthpc "GDP per capita growth, annual % (Source: WDI)"
    label var unpkm "Any UN PKM (Source: Third-Party Peacekeeping Missions)"
    label var conflict "Any conflict (Source: UCDP/PRIO)"
    label var evermajorconflict "Ever any conflict with more than 1000 fatalities in a year (Source: UCDP/PRIO)"
    label var past10conflict "Number of years in last decade with any conflict (Source: UCDP/PRIO)"
    label var past5conflict "Number of years in last half-decade with any conflict (Source: UCDP/PRIO)"
    label var lndeaths "Total battle deaths in current year, logged (Source: Battle Deaths Dataset)"
    label var lnpast5deaths "Total battle deaths in last five years, logged (Source: Battle Deaths Dataset)"
    label var gdp "GDP, constant 2005 US$ (Source: WDI)"
    label var lnpop "Population, logged (Source: WDI)"
    label var ethfrac "Ethnic fractionalization (Source: Fearon and Laitin, 2003)"
    label var lmtnest "Estimated % mountainous terrain, logged (Source: Fearon and Laitin, 2003)"
    label var colbrit "Former British colony (Source: Fearon and Laitin, 2003)"
    label var colfra "Former French colony (Source: Fearon and Laitin, 2003)"
    label var lndepunpkm "Total UN personnel deployed, logged (Source: Third-Party Peacekeeping Missions)"
    label var infantmort "Infant mortality (Source: WDI)"
    keep countryname countrynum year growthpc unpkm lndepunpkm conflict evermajorconflict past10conflict past5conflict lndeaths lnpast5deaths gdp lnpop ethfrac lmtnest colbrit colfra infantmort
    order countryname countrynum year growthpc unpkm lndepunpkm conflict evermajorconflict past10conflict past5conflict lndeaths lnpast5deaths gdp lnpop ethfrac lmtnest colbrit colfra infantmort
    compress

* label and save data
    label data "Replication macro-level data for Beber et al., ISQ"
    save BGGK_ISQ_data_macro, replace

* micro-data from Liberia

* load data
    use "Liberia/BGGK_Liberia_data", clear

* create variables for analysis

    * employee
    gen employee=1 if q97=="Yes"
    replace employee=0 if q97=="No"
    label var employee "Has worked as employee in previous 12 months (q97)"

    * share of employee's customer base that is foreign
    foreach i of var q103 q105 {
        replace `i'="" if `i'=="Don't know" | `i'=="Refuse"
        destring `i', gen(`i'num)
    }
    gen share_foreign=q105num/q103num
    replace share_foreign=. if share_foreign>1
    label var share_foreign "Share of customers in primary job that are foreign (q105/q103)"

    * high-level ILO occupation classification
    * see file "Numeric codes and values.dta" for details
    gen occupation=1 if q99==1 | (q99>10 & q99<16)
    replace occupation=2 if q99==2 | (q99>20 & q99<27)
    replace occupation=3 if q99==3 | (q99>30 & q99<37)
    replace occupation=4 if q99==4 | (q99>40 & q99<45)
    replace occupation=5 if q99==5 | (q99>50 & q99<55)
    replace occupation=6 if q99==6 | (q99>60 & q99<64)
    replace occupation=7 if q99==7 | (q99>70 & q99<76)
    replace occupation=8 if q99==8 | (q99>80 & q99<84)
    replace occupation=9 if q99==9 | (q99>90 & q99<97)
    label var occupation "Primary occupation, high-level classification (first digit of q99)"

    * owns business or earns money working for self
    gen business=1 if q48=="Yes"
    replace business=0 if q48=="No"
    label var business "Owns business or earns money working for self (q48)"

    * share of business's customer base that is foreign
    foreach i of var q59 q61 {
        replace `i'="" if `i'=="Don't know" | `i'=="Refuse"
        destring `i', gen(`i'num)
    }
    gen biz_foreign=q61num/q59num
    replace biz_foreign=. if biz_foreign>1
    label var biz_foreign "Share of customers in primary business that are foreign (q61/q59)"

    * business funding and problems
    gen biz_funding=q54
    label var biz_funding "Main source of funding to start primary business (q54)"
    gen biz_problem=q57
    label var biz_problem "Most serious problem in primary business (q57)"

    * earnings
    foreach i of varlist q52 q68 {
        gen `i'temp=`i'
        replace `i'temp="" if `i'temp=="Don't know" | `i'temp=="Refuse"
        destring `i'temp, replace
    }
    egen earnings=rowtotal(q52temp q68temp), missing
    gen lnearnings=log(earnings+1)
    replace lnearnings=0 if q48=="No" & lnearnings==.
    drop earnings q52temp q68temp
    label var lnearnings "Total log earnings from all enterprises in last month (q48, q52, q68)"

    * savings
    gen savings=1 if q47=="Yes"
    replace savings=0 if q47=="No"
    label var savings "Has savings (q47)"

    * assets
    foreach i of varlist q139 q141 {
        gen `i'temp=`i'
        replace `i'temp="" if `i'temp=="Don't know" | `i'temp=="Refuse"
        destring `i'temp, replace
    }
    egen assets=rowtotal(q139temp q141temp), missing
    gen lnassets=log(assets+1)
    drop assets q139temp q141temp
    label var lnassets "Total log assets (q139, q141)"

    * asset index
    local k=1
    foreach i in "House" "Flat" "Land (other than the land the house is on, if any)" "Radio / cassette player / MP3 player" "Bicycle" "Motorcycle / scooter" "Motor car" "Icebox" "Generator" "Television" "Table" "Chairs" "Cupboard" "Sewing machine" "Computer" "Boat or canoe" "Chickens" "Pigs" "Sheep" "Goats" "Milk Cows" "Other cattle" {
        gen asset`k'=0
        local suff a b c d e f g h i j k l m n o p
        foreach z of local suff {
            replace asset`k'=1 if q138`z'=="`i'"
        }
        local k=`k'+1
    }
    * drop rarely owned assets from index
    foreach i of varlist asset* {
        summ `i'
        if(`r(mean)'<.03) drop `i'
    }
    * factor analysis on remaining assets:
    * house, land (other than the land the house is on), radio/cassette player/MP3 player, motorcycle/scooter, motor car, icebox, generator, television, table, chairs, cupboard, computer, chickens, non-dairy cattle
    factor asset*, factors(1)
    predict asset_factor
    drop asset1-asset22
    label var asset_factor "Asset index, factor analysis (q138)"

    * adequate food and housing
    encode q134, gen(enough_food)
    encode q135, gen(enough_housing)
    label define enough 0 "Less than enough" 1 "Just enough" 2 "More than enough", replace
    foreach i of varlist enough_food enough_housing {
        recode `i' (1 5=.) (3=0) (2=1) (4=2)
        label val `i' enough
    }
    recast int enough_food enough_housing
    label var enough_food "Had enough food over past month (q134)"
    label var enough_housing "Enough housing for needs (q135)"

    * electricity
    gen electricity=1 if q143=="Yes"
    replace electricity=0 if q143=="No"
    label var electricity "Home has electricity (q143)"

    * ever sold a good or service to UNPK
    gen pk_sold=1 if regexm(q17, "Within") | q17=="Over a year ago"
    replace pk_sold=0 if q17=="Never"
    label var pk_sold "Ever sold a good or service to UNMIL PKO personnel (q17)"

    * sold a good or service to UNPK, categorical
    gen pk_sold_cat=0 if q17=="Never"
    replace pk_sold_cat=1 if q17=="Over a year ago"
    replace pk_sold_cat=2 if q17=="Within the last three months" | q17=="Within the last 6 months" | q17=="Within the last year"
    replace pk_sold_cat=3 if q17=="Within the last two weeks" | q17=="Within the last month" | q17=="Within the last week" | q17=="Within the last 24 hours"
    label define pk_sold_cat 0 "Never" 3 "Past month" 2 "Past year" 1 "Over a year ago"
    label val pk_sold_cat pk_sold_cat
    label var pk_sold_cat "Sold a good or service to UNMIL PKO personnel, categorical (q17)"

    * received money, food, medicines, clothing from UNMIL
    gen pk_money=1 if q28=="Yes"
    replace pk_money=0 if q28=="No"
    label var pk_money "Received money, food, medicines, clothing from UNMIL (q28)"

    * would you like UNMIL to stay
    gen pk_stay=1 if q38=="Yes"
    replace pk_stay=0 if q38=="No"
    label var pk_stay "Would like that UNMIL continues to stay in Liberia (q38)"

    * has UNMIL increased city/town's safety
    gen pk_safety=1 if q24=="Yes"
    replace pk_safety=0 if q24=="No"
    label var pk_safety "Presence of UNMIL personnel has increased the city/town's safety (q24)"

    * has UNMIL increased own safety
    gen pk_own_safety=1 if q25=="Yes"
    replace pk_own_safety=0 if q25=="No"
    label var pk_own_safety "Presence of UNMIL personnel has increased own personal security (q25)"

    * has UN military personnel made positive contribution other than security
    gen pk_positive=1 if q27!="" & q27!="Nothing" & q27!="Don't know" & q27!="Refuse"
    replace pk_positive=0 if q27=="Nothing" | q27=="Don't know"
    label var pk_positive "UN military personnel has made positive contribution other than security (q27)"

    * does UN military presence in city have negative consequences
    gen pk_no_negative=0 if q32=="Yes"
    replace pk_no_negative=1 if q32=="No"
    label var pk_no_negative "UN military presence in city has no negative consequences (q32)"

    * what kind of negative consequences
    for any a b c d e f g h other: gen pk_negative_X=q33X
    label var pk_negative_a "Negative consequences, response 1 (q33a)"
    label var pk_negative_b "Negative consequences, response 2 (q33b)"
    label var pk_negative_c "Negative consequences, response 3 (q33c)"
    label var pk_negative_d "Negative consequences, response 4 (q33d)"
    label var pk_negative_e "Negative consequences, response 5 (q33e)"
    label var pk_negative_f "Negative consequences, response 6 (q33f)"
    label var pk_negative_g "Negative consequences, response 7 (q33g)"
    label var pk_negative_h "Negative consequences, response 8 (q33h)"
    label var pk_negative_other "Negative consequences, other response (q33other)"

    * gender
    gen female=1 if q2=="Woman"
    replace female=0 if q2=="Man"
    label var female "Female (q2)"

    * age
    gen age=q1
    label var age "Age (q1)"

    * marital status
    gen married=1 if q3=="Currently married monogamously" | q3=="Currently married polygamously"
    replace married=0 if q3=="Never married" | q3=="Not married (divorced/separated)" | q3=="Not married (widowed)"
    label var married "Married at time of survey (q3)"

    * education
    gen education = 0 if r_education==99
    replace education = r_education + 1 if education==.
    replace education = . if r_education==20
    label var education "Education level (r_education)"

    * language
    gen lang_bassa=1 if q4=="Bassa"
    replace lang_bassa=0 if q4!="Bassa" & q4!=""
    label var lang_bassa "Language spoken at home: Bassa (q4)"
    
    gen lang_english=1 if q4=="English"
    replace lang_english=0 if q4!="English" & q4!=""
    label var lang_english "Language spoken at home: English (q4)"
    
    gen lang_gbandi=1 if q4=="Gbandi"
    replace lang_gbandi=0 if q4!="Gbandi" & q4!=""
    label var lang_gbandi "Language spoken at home: Gbandi (q4)"
    
    gen lang_gio=1 if q4=="Gio"
    replace lang_gio=0 if q4!="Gio" & q4!=""
    label var lang_gio "Language spoken at home: Gio (q4)"
    
    gen lang_grebo=1 if q4=="Grebo"
    replace lang_grebo=0 if q4!="Grebo" & q4!=""
    label var lang_grebo "Language spoken at home: Grebo (q4)"
    
    gen lang_kpelle=1 if q4=="Kpelle"
    replace lang_kpelle=0 if q4!="Kpelle" & q4!=""
    label var lang_kpelle "Language spoken at home: Kpelle (q4)"
    
    gen lang_krahn=1 if q4=="Krahn"
    replace lang_krahn=0 if q4!="Krahn" & q4!=""
    label var lang_krahn "Language spoken at home: Krahn (q4)"
    
    gen lang_kru=1 if q4=="Kru"
    replace lang_kru=0 if q4!="Kru" & q4!=""
    label var lang_kru "Language spoken at home: Kru (q4)"
    
    gen lang_lorma=1 if q4=="Lorma"
    replace lang_lorma=0 if q4!="Lorma" & q4!=""
    label var lang_lorma "Language spoken at home: Lorma (q4)"
    
    gen lang_mandingo=1 if q4=="Mandingo"
    replace lang_mandingo=0 if q4!="Mandingo" & q4!=""
    label var lang_mandingo "Language spoken at home: Mandingo (q4)"
    
    gen lang_mano=1 if q4=="Mano"
    replace lang_mano=0 if q4!="Mano" & q4!=""
    label var lang_mano "Language spoken at home: Mano (q4)"
    
    gen lang_vai=1 if q4=="Vai"
    replace lang_vai=0 if q4!="Vai" & q4!=""
    label var lang_vai "Language spoken at home: Vai (q4)"
    
    gen lang_other=1 if q4=="Belle" | q4=="Gola" | q4=="Kissi" | q4=="Mende" | q4=="Other"
    replace lang_other=0 if lang_other==. & q4!=""
    label var lang_other "Language spoken at home: Other, e.g. Belle, Gola, Kissi, Mende (q4)"

    * cognitive score
    gen test1=1 if q6=="Correct"
    replace test1=0 if q6=="Incorrect"
    gen test2=1 if q7=="Second"
    replace test2=0 if q7=="First" | regexm(q7, "Other")
    gen test3=1 if q8=="Correct"
    replace test3=0 if q8=="Incorrect"
    gen test4=1 if q9=="Red"
    replace test4=0 if q9=="Don't know" | regexm(q9, "Other")
    gen test5=1 if q10=="Correct"
    replace test5=0 if q10=="Incorrect"
    gen test6=1 if q11=="Correct"
    replace test6=0 if q11=="Incorrect"
    * install code for Mokken scale procedure, if necessary
    * ssc install msp
    msp test*
    gen score=test1+test2+test3
    label var score "Cognitive score (q6-q8)"
    drop test*

    * location in 1999
    gen location_1999=q264
    label var location_1999 "Location in January 1999 (q264)"

    * deaths and injury of working-age individuals (15-64, as defined by World Bank)
    * prepare variables for year and age at death or injury
    foreach j of varlist q160 q167 q174 q181 q195 q201 q207 {
            destring `j', gen(`j'_numeric) force
    }
    * some variables were already numeric
    gen q188_numeric=q188
    gen q201_numeric=q201
    gen q207_numeric=q207
    * any deaths of working-age (15-64) individuals in household by accident, natural disasters, non-war crime (not disease or old age) in previous three years
    gen hh_death=0
    foreach i of numlist 2009/2012 {
        replace hh_death=1 if q161year=="`i'" & q160_numeric>14 & q160_numeric<65 & (q158=="Flood / drought / natural disaster" | q158=="Non-war-related violence / criminals" | q158=="Accident")
        replace hh_death=1 if q168year=="`i'" & q167_numeric>14 & q167_numeric<65 & (q165=="Flood / drought / natural disaster" | q165=="Non-war-related violence / criminals" | q165=="Accident")
        replace hh_death=1 if q175year=="`i'" & q174_numeric>14 & q174_numeric<65 & (q172=="Flood / drought / natural disaster" | q172=="Non-war-related violence / criminals" | q172=="Accident")
        replace hh_death=1 if q182year=="`i'" & q181_numeric>14 & q181_numeric<65 & (q179=="Flood / drought / natural disaster" | q179=="Non-war-related violence / criminals" | q179=="Accident")
        replace hh_death=1 if q189year=="`i'" & q188_numeric>14 & q188_numeric<65 & (q186=="Flood / drought / natural disaster" | q186=="Non-war-related violence / criminals" | q186=="Accident")
    }
    label var hh_death "Any accidental deaths of working-age person in prior three years (q160-q189)"
    * also include injuries
    gen hh_death_injury=hh_death
    foreach i of numlist 2009/2012 { 
        replace hh_death_injury=1 if q196year=="`i'" & q195_numeric>14 & q195_numeric<65 & (q194=="Flood / drought / natural disaster" | q194=="Non-war-related violence / criminals" | q194=="Accident")
        replace hh_death_injury=1 if q202year=="`i'" & q201_numeric>14 & q201_numeric<65 & (q200=="Flood / drought / natural disaster" | q200=="Non-war-related violence / criminals" | q200=="Accident")
        replace hh_death_injury=1 if q208year==`i' & q207_numeric>14 & q207_numeric<65 & (q206=="Flood / drought / natural disaster" | q206=="Non-war-related violence / criminals" | q206=="Accident")
    }
    label var hh_death_injury "Any accidental deaths/injury of working-age person in three years (q160-q208)"
    drop *_numeric

* retain variables for analysis
    keep psu pweight fpc1 fpc2 stratum hh_size employee-hh_death_injury
    order psu pweight fpc1 fpc2 stratum employee-married hh_size education-hh_death_injury
    compress

* label and save data
    label data "Replication micro-level data for Beber et al., ISQ"
    save BGGK_ISQ_data_micro, replace
